package com.hdc.hdc.warehouseManage.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.hdc.hdc.warehouseManage.entity.GoodsLocation;
import com.hdc.hdc.warehouseManage.entity.House;
import com.hdc.hdc.warehouseManage.entity.Inorder;
import com.hdc.hdc.warehouseManage.entity.InorderAndGoods;
import com.hdc.hdc.warehouseManage.entity.Position;
import com.hdc.hdc.warehouseManage.exception.MyException;
import com.hdc.hdc.warehouseManage.repository.GoodsLocationRepository;
import com.hdc.hdc.warehouseManage.repository.HouseRepository;
import com.hdc.hdc.warehouseManage.repository.InorderAndGoodsRepository;
import com.hdc.hdc.warehouseManage.repository.InorderRepository;
import com.hdc.hdc.warehouseManage.repository.PositionRepository;
import com.hdc.hdc.warehouseManage.service.UploadServiceI;
import com.hdc.hdc.warehouseManage.utill.IsNotNullValit;
import com.hdc.hdc.warehouseManage.utill.ParseExcelUtil;

/**
 * 上传服务类
 * @author GJY
 * 人生格言：学以致用终将成功
 * @version 创建时间：2018年4月26日 上午10:52:42 
 *
 */
@Service
public class UploadServiceImpl implements UploadServiceI {
	
	@Autowired
	private InorderRepository inorderRepository;
	
	@Autowired
	private InorderAndGoodsRepository inorderAndGoodsRepository;
	
	@Autowired
	private HouseRepository houseRepository;
	
	@Autowired
	private PositionRepository positionRepository;
	
	@Autowired
	private GoodsLocationRepository goodsLocationRepository;

	@Override
	public void saveData(Inorder inorder, MultipartFile file) {
		
		List<InorderAndGoods> iag = new ArrayList<InorderAndGoods>(50);
		
		iag = ParseExcelUtil.parseExcel(inorder,file);

		if(IsNotNullValit.StringIsNotNull(inorder.getToWhereHouseName())) {
			
			List<House> houses = this.houseRepository.findByHouseName(inorder.getToWhereHouseName());
			
			if (IsNotNullValit.CollectionIsNotNull(houses)) {
				throw new MyException("仓库:"+inorder.getToWhereHouseName()+"不存在");
			}
			
			inorder.setToWhereHouse(houses.get(0).getId());
		}
		
		
		if(IsNotNullValit.StringIsNotNull(inorder.getToWherePositionName())) {
			
			List<Position> positions = this.positionRepository.findByHouseIdAndPositionName(inorder.getToWhereHouse(),inorder.getToWherePositionName());
			if (IsNotNullValit.CollectionIsNotNull(positions)) {
				throw new MyException("仓库:"+inorder.getToWhereHouseName()+"不存在库位:"+inorder.getToWherePositionName());
			}
			inorder.setToWherePosition(positions.get(0).getPositionName());
		}
		
		
		if (IsNotNullValit.StringIsNotNull(inorder.getToWhereLocationName())) {
			List<GoodsLocation> locations = this.goodsLocationRepository.findByLocationName(inorder.getToWhereLocationName());
			if (IsNotNullValit.CollectionIsNotNull(locations)) {
				throw new MyException("没有:"+inorder.getToWhereLocationName()+"这个货位");
			}
			inorder.setToWhereLocation(locations.get(0).getId());
		}
		
		inorderRepository.save(inorder);
		
		inorderAndGoodsRepository.saveAll(iag);
		
	}

}
